<?php

namespace App\Console\Commands\OpenShop\Contract;

use App\Enums\OpenShop\InvestmentPromotion\ContractStatusEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use App\Service\OpenShop\Feishu\UserExtContractService;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class FixUserContractCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'contract:fixUserContract';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '合同：填充历史合同分类id';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        OpenShopUserContractModel::query()
//                                 ->where("orderNo", "KD2022101461453860")
                                 ->where("contractExtNo", "<>", "")
                                 ->orderBy("id", "asc")
                                 ->chunkById(100, function (Collection $collection) {
                                     $collection->each(function (OpenShopUserContractModel $contractModel) {
                                         $this->getOutput()->info("[ 门店编号 ] [ {$contractModel->orderNo} ]");
                                         $contractDetail              = UserExtContractService::getContractDetailByExtContractId($contractModel->contractExtNo);
                                         $detailStatus                = attributeGet($contractDetail,
                                             "result.status", "");
                                         $contractModel->categoryId   = attributeGet($contractDetail,
                                             "result.category.id", "");
                                         $contractModel->categoryName = attributeGet($contractDetail,
                                             "result.category.name", "");
                                         $contractModel->detailStatus = $detailStatus;
                                         if ($detailStatus == ContractStatusEnum::COMPLETE) {
                                             $signTimeFormat              = attributeGet(
                                                 collect(attributeGet($contractDetail,
                                                     "result.signatories", []))->last(),
                                                 "complateTime",
                                                 null
                                             );
                                             $contractModel->signTime = $signTimeFormat ? strtotime($signTimeFormat) : 0;
                                         }
                                         $contractModel->save();
                                     });
                                 });
        return 0;
    }
}
